package com.shjn.squirrel.model;

import com.shjn.squirrel.application.MyApplication;
import com.shjn.squirrel.bean.storageBean.WastePrice;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * 用户信息缓存
 */
public class UserInfoCache {

    private UserInfoCache() {
    }

    /**
     * 用户登录状态
     */
    public static Boolean loginState = false;

    /**
     * 用户手机号
     */
    public static String telNum;

    /**
     * 用户选择的垃圾种类
     */
    public static String category;

    /**
     * 用户当前投递的垃圾量
     */
    public static double nowDeliverQuantity = 0;

    /**
     * 用户投递记录
     */
    public static LinkedHashMap<String, ArrayList<Double>> records = new LinkedHashMap<>();// 0:weight 1:count

    /**
     * 添加用户投递记录
     *
     * @param quantity
     */
    public static void addRecord(double quantity) {
        if (UserInfoCache.category.equals(Constant.bottle)) {//饮料瓶
            ArrayList<Double> item = new ArrayList<>();
            item.add(0, 0.0);
            if (UserInfoCache.records.containsKey(UserInfoCache.category)) {
                item.add(1, UserInfoCache.records.get(UserInfoCache.category).get(1) + quantity);
            } else {
                item.add(1, quantity);
            }
            UserInfoCache.records.put(UserInfoCache.category, item);

        } else if (UserInfoCache.category.equals(Constant.glass) || UserInfoCache.category.equals(Constant.harm)) {//玻璃或有害
            ArrayList<Double> item = new ArrayList<>();
            if (UserInfoCache.records.containsKey(UserInfoCache.category)) {
                item.add(0, UserInfoCache.records.get(UserInfoCache.category).get(0) + quantity);
                item.add(1, UserInfoCache.records.get(UserInfoCache.category).get(0) + 1);
            } else {
                item.add(0, quantity);
                item.add(1, 1.0);
            }
            UserInfoCache.records.put(UserInfoCache.category, item);

        } else {//其它垃圾
            ArrayList<Double> item = new ArrayList<>();
            if (UserInfoCache.records.containsKey(UserInfoCache.category)) {
                item.add(0, UserInfoCache.records.get(UserInfoCache.category).get(0) + quantity);
            } else {
                item.add(0, quantity);
            }
            item.add(1, 0.0);
            UserInfoCache.records.put(UserInfoCache.category, item);


        }

    }

    /**
     * 获取总环保金
     *
     * @return
     */
    public static double getTotalMoney() {
        double totalMoney = 0;
        ArrayList<WastePrice> wastePriceList = LocalStorage.getWastePrice(MyApplication.getInstance());
        for (String key : UserInfoCache.records.keySet()) {
            if (!key.equals(Constant.glass) && !key.equals(Constant.harm)) {
                if (key.equals(Constant.bottle)) {
                    totalMoney += wastePriceList.get(Constant.categoryMap.get(key)[3] - 1).getUserPrice() * UserInfoCache.records.get(key).get(1);
                } else {
                    totalMoney += wastePriceList.get(Constant.categoryMap.get(key)[3] - 1).getUserPrice() * UserInfoCache.records.get(key).get(0);
                }
            }
        }
        return totalMoney;
    }

    /**
     * 获取总积分
     *
     * @return
     */
    public static double getTotalIntegral() {
        double totalIntegral = 0;
        ArrayList<WastePrice> wastePriceList = LocalStorage.getWastePrice(MyApplication.getInstance());
        for (String key : UserInfoCache.records.keySet()) {
            if (key.equals(Constant.glass) || key.equals(Constant.harm)) {
                totalIntegral += wastePriceList.get(Constant.categoryMap.get(key)[3] - 1).getUserPrice() * UserInfoCache.records.get(key).get(1);
            }
        }
        return totalIntegral;
    }

    /**
     * 清空本次用户信息
     */
    public static void clearUserInfo() {
        loginState = false;
        telNum = null;
        category = null;
        nowDeliverQuantity = 0;
        UserInfoCache.records.clear();
    }

}
